{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "aecb5375",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第16020期开奖结果录入完成\n",
      "第16019期开奖结果录入完成\n",
      "第16018期开奖结果录入完成\n",
      "第16017期开奖结果录入完成\n",
      "第16016期开奖结果录入完成\n",
      "第16015期开奖结果录入完成\n",
      "第16014期开奖结果录入完成\n",
      "第16013期开奖结果录入完成\n",
      "第16012期开奖结果录入完成\n",
      "第16011期开奖结果录入完成\n",
      "第16010期开奖结果录入完成\n",
      "第16009期开奖结果录入完成\n",
      "第16008期开奖结果录入完成\n",
      "第16007期开奖结果录入完成\n",
      "第16006期开奖结果录入完成\n",
      "第16005期开奖结果录入完成\n",
      "第16004期开奖结果录入完成\n",
      "第16003期开奖结果录入完成\n",
      "第16002期开奖结果录入完成\n",
      "第16001期开奖结果录入完成\n",
      "第15154期开奖结果录入完成\n",
      "第15153期开奖结果录入完成\n",
      "第15152期开奖结果录入完成\n",
      "第15151期开奖结果录入完成\n",
      "第15150期开奖结果录入完成\n",
      "第15149期开奖结果录入完成\n",
      "第15148期开奖结果录入完成\n",
      "第15147期开奖结果录入完成\n",
      "第15146期开奖结果录入完成\n",
      "第15145期开奖结果录入完成\n",
      "第15144期开奖结果录入完成\n",
      "第15143期开奖结果录入完成\n",
      "第15142期开奖结果录入完成\n",
      "第15141期开奖结果录入完成\n",
      "第15140期开奖结果录入完成\n",
      "第15139期开奖结果录入完成\n",
      "第15138期开奖结果录入完成\n",
      "第15137期开奖结果录入完成\n",
      "第15136期开奖结果录入完成\n",
      "第15135期开奖结果录入完成\n",
      "第15134期开奖结果录入完成\n",
      "第15133期开奖结果录入完成\n",
      "第15132期开奖结果录入完成\n",
      "第15131期开奖结果录入完成\n",
      "第15130期开奖结果录入完成\n",
      "第15129期开奖结果录入完成\n",
      "第15128期开奖结果录入完成\n",
      "第15127期开奖结果录入完成\n",
      "第15126期开奖结果录入完成\n",
      "第15125期开奖结果录入完成\n",
      "第15124期开奖结果录入完成\n",
      "第15123期开奖结果录入完成\n",
      "第15122期开奖结果录入完成\n",
      "第15121期开奖结果录入完成\n",
      "第15120期开奖结果录入完成\n",
      "第15119期开奖结果录入完成\n",
      "第15118期开奖结果录入完成\n"
     ]
    },
    {
     "ename": "PermissionError",
     "evalue": "[Errno 13] Permission denied: '双色球开奖结果.csv'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mPermissionError\u001b[0m                           Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[4], line 45\u001b[0m\n\u001b[0;32m     41\u001b[0m     turn_page()\n\u001b[0;32m     44\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;18m__name__\u001b[39m \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m__main__\u001b[39m\u001b[38;5;124m'\u001b[39m:\n\u001b[1;32m---> 45\u001b[0m     \u001b[43mmain\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
      "Cell \u001b[1;32mIn[4], line 41\u001b[0m, in \u001b[0;36mmain\u001b[1;34m()\u001b[0m\n\u001b[0;32m     39\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mexists(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m双色球开奖结果2.csv\u001b[39m\u001b[38;5;124m'\u001b[39m):\n\u001b[0;32m     40\u001b[0m     os\u001b[38;5;241m.\u001b[39mremove(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m双色球开奖结果2.csv\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m---> 41\u001b[0m \u001b[43mturn_page\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
      "Cell \u001b[1;32mIn[4], line 35\u001b[0m, in \u001b[0;36mturn_page\u001b[1;34m()\u001b[0m\n\u001b[0;32m     33\u001b[0m url \u001b[38;5;241m=\u001b[39m p[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mhref\u001b[39m\u001b[38;5;124m'\u001b[39m]\n\u001b[0;32m     34\u001b[0m page \u001b[38;5;241m=\u001b[39m p\u001b[38;5;241m.\u001b[39mstring\n\u001b[1;32m---> 35\u001b[0m \u001b[43mdownload\u001b[49m\u001b[43m(\u001b[49m\u001b[43murl\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpage\u001b[49m\u001b[43m)\u001b[49m\n",
      "Cell \u001b[1;32mIn[4], line 13\u001b[0m, in \u001b[0;36mdownload\u001b[1;34m(url, page)\u001b[0m\n\u001b[0;32m     11\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m li \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mlist\u001b[39m:\n\u001b[0;32m     12\u001b[0m     ball\u001b[38;5;241m.\u001b[39mappend(li\u001b[38;5;241m.\u001b[39mstring)\n\u001b[1;32m---> 13\u001b[0m \u001b[43mwrite_to_excel\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpage\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mball\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     14\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m第\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mpage\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m期开奖结果录入完成\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "Cell \u001b[1;32mIn[4], line 18\u001b[0m, in \u001b[0;36mwrite_to_excel\u001b[1;34m(page, ball)\u001b[0m\n\u001b[0;32m     17\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mwrite_to_excel\u001b[39m(page, ball):\n\u001b[1;32m---> 18\u001b[0m     f \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mopen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m双色球开奖结果.csv\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43ma\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mencoding\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mutf_8_sig\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m     19\u001b[0m     f\u001b[38;5;241m.\u001b[39mwrite(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m第\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mpage\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m期,\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mball[\u001b[38;5;241m0\u001b[39m]\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m,\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mball[\u001b[38;5;241m1\u001b[39m]\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m,\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mball[\u001b[38;5;241m2\u001b[39m]\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m,\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mball[\u001b[38;5;241m3\u001b[39m]\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m,\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mball[\u001b[38;5;241m4\u001b[39m]\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m,\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mball[\u001b[38;5;241m5\u001b[39m]\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m,\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mball[\u001b[38;5;241m6\u001b[39m]\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m     20\u001b[0m     f\u001b[38;5;241m.\u001b[39mclose()\n",
      "File \u001b[1;32mD:\\anaconda\\lib\\site-packages\\IPython\\core\\interactiveshell.py:282\u001b[0m, in \u001b[0;36m_modified_open\u001b[1;34m(file, *args, **kwargs)\u001b[0m\n\u001b[0;32m    275\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m file \u001b[38;5;129;01min\u001b[39;00m {\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m}:\n\u001b[0;32m    276\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[0;32m    277\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIPython won\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt let you open fd=\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfile\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m by default \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    278\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mas it is likely to crash IPython. If you know what you are doing, \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    279\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124myou can use builtins\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m open.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    280\u001b[0m     )\n\u001b[1;32m--> 282\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m io_open(file, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
      "\u001b[1;31mPermissionError\u001b[0m: [Errno 13] Permission denied: '双色球开奖结果.csv'"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import os\n",
    "from bs4 import BeautifulSoup\n",
    " \n",
    " \n",
    "def download(url, page):\n",
    "    html = requests.get(url).text\n",
    "    soup = BeautifulSoup(html, 'html.parser')\n",
    "    list = soup.select('div.ball_box01 ul li')\n",
    "    ball = []\n",
    "    for li in list:\n",
    "        ball.append(li.string)\n",
    "    write_to_excel(page, ball)\n",
    "    print(f\"第{page}期开奖结果录入完成\")\n",
    " \n",
    " \n",
    "def write_to_excel(page, ball):\n",
    "    f = open('双色球开奖结果.csv', 'a', encoding='utf_8_sig')\n",
    "    f.write(f'第{page}期,{ball[0]},{ball[1]},{ball[2]},{ball[3]},{ball[4]},{ball[5]},{ball[6]}\\n')\n",
    "    f.close()\n",
    " \n",
    " \n",
    "def turn_page():\n",
    "    url = \"http://kaijiang.500.com/ssq.shtml\"\n",
    "    html = requests.get(url).text\n",
    "    soup = BeautifulSoup(html, 'html.parser')\n",
    "    pageList = soup.select(\"div.iSelectList a\")\n",
    " \n",
    "    # 获取最近30期的页码\n",
    "    recent_pages = pageList[:4000]\n",
    " \n",
    "    for p in recent_pages:\n",
    "        url = p['href']\n",
    "        page = p.string\n",
    "        download(url, page)\n",
    " \n",
    " \n",
    "def main():\n",
    "    if os.path.exists('双色球开奖结果.csv'):\n",
    "        os.remove('双色球开奖结果.csv')\n",
    "    turn_page()\n",
    " \n",
    " \n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
